package com.fx.usercenter.exception;

import com.fx.usercenter.common.BaseResponse;
import com.fx.usercenter.common.ErrorCode;
import com.fx.usercenter.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
//  可以直接将返回值变为json格式返回给前端，屏蔽掉后端细节,
@Slf4j
public class GlobalException {
 @ExceptionHandler(BusinessException.class)
    public BaseResponse businessException(BusinessException e){
     log.info("businessException" +e.getMessage()+e);
     return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
 }
// 使用@ExceptionHandler注解为对应的异常配置异常处理器
 @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeException(RuntimeException e){
        log.info("runtimeException" +e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage(),"");
    }
}
